1   /*
2    * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /*
25   * @test
26   * @author Weijun Wang
27   * @bug 6418422
28   * @bug 6418425
29   * @bug 6418433
30   * @summary ObjectIdentifier should reject 1.2.3.-4 and throw IOException on all format errors
31   */
32  
33  import java.io.IOException;
34  import java.security.NoSuchAlgorithmException;
35  import org.ietf.jgss.GSSException;
36  import org.ietf.jgss.Oid;
37  import javax.crypto.EncryptedPrivateKeyInfo;
38  import sun.security.util.*;
39  import java.util.Arrays;
40  
41  public class OidFormat {
42      public static void main(String[] args) throws Exception {
43          String[] badOids = {
44              // number problems
45              "0", "1", "2",
46              "3.1.1", "3", "4",
47              "1.40", "1.111.1",
48              "-1.2", "0,-2", "1.-2", "2.-2",
49              "1.2.-3.4", "1.2.3.-4",
50              // format problems
51              "aa", "aa.aa",
52              "", "....", "1.2..4", "1.2.3.", "1.", "1.2.", "0.1.",
53              "1,2",
54          };
55  
56          for (String s: badOids) {
57              testBad(s);
58          }
59  
60          String[] goodOids = {
61              "0.0", "0.1", "1.0", "1.2",
62              "0.39", "1.39", "2.47", "2.40.3.6", "2.100.3", "2.123456.3",
63              "1.2.3", "1.2.3445",
64              "1.3.6.1.4.1.42.2.17",
65              // 4811968: ASN.1 cannot handle huge OID components
66              "2.16.764.1.3101555394.1.0.100.2.1",
67              "2.2726957624935694386592435",  // as huge as possible
68              "1.2.777777777777777777",
69              "1.2.888888888888888888.111111111111111.2222222222222.33333333333333333.44444444444444",
70              "1.2." +
71                  "1111111111111111111111111111111111111111111111111111111111111." +
72                  "2222222222222222222222222222222222222222222222222222222222222222." +
73                  "333333333333333333333333333333333333333333333333333333333333333." +
74                  "4444444444444444444444444444444444444444444444444444444." +
75                  "55555555555555555555555555555555555555555555555555555555555555555555555." +
76                  "666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666." +
77                  "77777777777777777777777777777777777777777777777777777777777777777777777777." +
78                  "8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888." +
79                  "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
80              "1.2.2147483647.4",
81              "1.2.268435456.4",
82          };
83  
84          for (String s: goodOids) {
85              testGood(s);
86          }
87  
88          int[][] goodInts = {
89              {0,0}, {0,1}, {1,0}, {1,2},
90              {0,39}, {1,39}, {2,47}, {2,40,3,6}, {2,100,3}, {2,123456,3},
91              {1,2,3}, {1,2,3445},
92              {1,3,6,1,4,1,42,2,17},
93          };
94  
95          for (int[] is: goodInts) {
96              testGood(is);
97          }
98  
99          int[][] badInts = new int[][] {
100             {0}, {1}, {2},
101             {3,1,1}, {3}, {4},
102             {1,40}, {1,111,1},
103             {-1,2}, {0,-2}, {1,-2}, {2,-2},
104             {1,2,-3,4}, {1,2,3,-4},
105         };
106 
107         for (int[] is: badInts) {
108             testBad(is);
109         }
110 
111     }
112 
113     static void testBad(int[] ints) throws Exception {
114         System.err.println("Trying " + Arrays.toString(ints));
115         try {
116             new ObjectIdentifier(ints);
117             throw new Exception("should be invalid ObjectIdentifier");
118         } catch (IOException ioe) {
119             System.err.println(ioe);
120         }
121     }
122 
123     static void testGood(int[] ints) throws Exception {
124         System.err.println("Trying " + Arrays.toString(ints));
125         ObjectIdentifier oid = new ObjectIdentifier(ints);
126         DerOutputStream os = new DerOutputStream();
127         os.putOID(oid);
128         DerInputStream is = new DerInputStream(os.toByteArray());
129         ObjectIdentifier oid2 = is.getOID();
130         if (!oid.equals((Object)oid2)) {
131             throw new Exception("Test DER I/O fails: " + oid + " and " + oid2);
132         }
133     }
134 
135     static void testGood(String s) throws Exception {
136         System.err.println("Trying " + s);
137         ObjectIdentifier oid = new ObjectIdentifier(s);
138         if (!oid.toString().equals(s)) {
139             throw new Exception("equal test fail");
140         }
141         DerOutputStream os = new DerOutputStream();
142         os.putOID(oid);
143         DerInputStream is = new DerInputStream(os.toByteArray());
144         ObjectIdentifier oid2 = is.getOID();
145         if (!oid.equals((Object)oid2)) {
146             throw new Exception("Test DER I/O fails: " + oid + " and " + oid2);
147         }
148     }
149 
150     static void testBad(String s) throws Exception {
151         System.err.println("Trying " + s);
152         try {
153             new ObjectIdentifier(s);
154             throw new Exception("should be invalid ObjectIdentifier");
155         } catch (IOException ioe) {
156             System.err.println(ioe);
157         }
158 
159         try {
160             new Oid(s);
161             throw new Exception("should be invalid Oid");
162         } catch (GSSException gsse) {
163             ;
164         }
165 
166         try {
167             new EncryptedPrivateKeyInfo(s, new byte[8]);
168             throw new Exception("should be invalid algorithm");
169         } catch (NoSuchAlgorithmException e) {
170             ;
171         }
172     }
173 }